package com.example.aircraftwar.codes.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.aircraftwar.R;
import com.example.aircraftwar.codes.goal.Record;
import com.example.aircraftwar.codes.goal2.Record2;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class RegActivity extends AppCompatActivity {
    public EditText Name, Password, Password2;
    private List<Record2> records = new ArrayList<>();
    //private HashSet<Record2> records = new HashSet<>();
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    private boolean isExist;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);
        Name = findViewById(R.id.name);
        Password = findViewById(R.id.password);
        Password2 = findViewById(R.id.password2);
    }


    public void reg(View view) {
        String sName = Name.getText().toString();
        String sPassword = Password.getText().toString();
        String sPassword2 = Password2.getText().toString();
        if (sPassword.equals(sPassword2)&& !sName.equals("0"))
        {
            isExist = false;
            Record2 record2 = new Record2(sName,sPassword);
            initRecords();
            for(Record2 instance:records){
                if(Objects.equals(instance.getName(), sName) && Objects.equals(instance.getPassword(), sPassword)){
                    isExist = true;
                    break;
                }
            }
            if(isExist){
                Toast toast=Toast.makeText(this,"出现错误，该账户已存在！",Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER,0,50);
                toast.show();
            }else{
                records.add(record2);
                saveRecords();
                returnLogin();
            }
        }
        else
        {
            Toast toast=Toast.makeText(this,"出现错误，可能是两次输入的密码不一致",Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER,0,50);
            toast.show();
        }
    }

    private void returnLogin() {
        Intent intent = new Intent();
        intent.setClass(this, LoginActivity.class);
        startActivity(intent);
    }

    private void initRecords(){
        sp = getSharedPreferences("account",MODE_PRIVATE);
        String goals_json = sp.getString("goals2",null);
        if(null == goals_json){
            records.clear();
        } else {
            Gson gson = new Gson();
            records = gson.fromJson(goals_json,new TypeToken<List<Record2>>(){}.getType());
            /*for (Record2 instance:records){
                System.out.println(instance.getName()+" - "+instance.getPassword());
            }*/
        }
    }

    private void saveRecords(){
        Gson gson = new Gson();
        String goals_json = gson.toJson(records);
        editor = getSharedPreferences("account",MODE_PRIVATE).edit();
        editor.clear();
        editor.putString("goals2",goals_json);
        editor.commit();
    }
}